//
//  UUID+SGExtension.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/11/14.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import Foundation
import KeychainAccess

extension UUID {
    static let KEYCHAIN_SERVICE: String = "com.spadger.KeychainAccess"
    static let IMEI_KEY: String = "IMEI"
    
    static func getUUID() -> String {
        let keychain = Keychain(service: KEYCHAIN_SERVICE)
        var uuid:String = ""
        do {
            uuid = try keychain.get(IMEI_KEY) ?? ""
        }
        catch let error {
            print(error)
        }
        if uuid.isEmpty {
            uuid = UUID().uuidString
            do {
                try keychain.set(uuid, key: IMEI_KEY)
            }
            catch let error {
                print(error)
                uuid = ""
            }
        }
        return uuid
    }
}
